<?php
	//include_once('verifica.php');
	include_once('config/class.pdo.php');
	
	//print_r($_POST[id]);
	if(isset($_POST['id'])) {
		$pdo = new DB;	
		$pdo-> conexao();
		
		$Query = 'SELECT 
					  ibgeMunicipio,
					  cidade,
					  fk_ibgeEstado,
					  uf
				  FROM municipio 
				  LEFT JOIN estado ON estado.ibgeEstado = municipio.fk_ibgeEstado
		WHERE ibgeMunicipio = '.$_POST['id'].'';
		$result = $pdo->oneReg($Query);				
		//print_r($result);
	} 			
?>
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<link rel="stylesheet" href="css/jquery-ui.css" />
		<link rel="stylesheet" href="css/style.css" />
		<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
		<script type="text/javascript" src="js/jquery-ui.js" ></script>		
		<script type="text/javascript" src="js/jquery.validate.js" ></script>
		<script type="text/javascript" src="js/jsCadMunicipio.js"></script>	
		<script type="text/javascript" src="js/funcoes.js"></script>			
	</head>	
	<body style="font-size:10px;">
		<form name="frmMunicipio" id="frmMunicipio" method="POST">
			<div id="divMunicipio" class="divMunicipio">
				<fieldset class="backFieldset">
					<legend> <b> Cadastro de Município </b> </legend>
					<table class="table">
						<tr>
							<td> Código IBGE: </td>
							<td colspan=3> <input type="text" name="cm_ibge" id="cm_ibge" title="Campo IBGE deve ser preenchido." value="<?php echo @$result['ibgeMunicipio']; ?>" required /> </td>
						</tr>
						<tr>
							<td> Cidade: </td>
							<td colspan=3> <input type="text" name="cm_cidade" id="cm_cidade" title="Campo CIDADE deve ser preenchido." value="<?php echo @$result['cidade']; ?>" required size="50" /> </td>
						</tr>
						<tr>
							<td> Estado: </td>
							<td colspan=3> 
								<select id="cm_selectEstado" name="cm_selectEstado" required > 
									<option value=""> -- Selecione -- </option>
									<?php
										$pdo = new DB;	
										$pdo-> conexao();
										
										$Query = 'SELECT 
													  ibgeEstado,
													  estado,																 
													  uf
												  FROM estado
												  ORDER BY uf';
										$resultEstados = $pdo->select($Query);	
										
										foreach($resultEstados as $row) {
											if (@$result['fk_ibgeEstado'] == $row['ibgeEstado']) {
												echo '<option value="'.$row['ibgeEstado'].'" selected >'.$row['uf'].' - '.utf8_encode($row['estado']).'</option>';
											} else {
												echo '<option value="'.$row['ibgeEstado'].'">'.$row['uf'].' - '.utf8_encode($row['estado']).'</option>';
											}
										}													
									?>
								</select>
							</td>
						</tr>						
					</table>
				</fieldset>
			</div>
			<?php
				echo '<table>';
					echo '<tr>';
						if(isset($_POST['id'])) {
							echo '<td> <button type="submit" name="atualizar" class="btn btn-success"> <i class="icon-refresh"></i> Atualizar </button>'; 
							echo '    <button type="button" name="cancelar" onclick="apagaForm()" class="btn btn-danger" > <i class="icon-ban-circle"></i> Cancelar </button>';
							echo '    <button type="button" name="voltar" id="voltar" onclick="loadPage(3);" class="btn btn-primary" > <i class="icon-arrow-left"></i> Voltar </button> <input type="hidden" id="controle" value="atualizar"> </td>';
						} else {
							echo '<td> <button type="submit" name="gravar" class="btn btn-success"> <i class="icon-ok"></i> Gravar </button>'; 
							echo '    <button type="button" name="cancelar" onclick="apagaForm()" class="btn btn-danger" > <i class="icon-ban-circle"></i> Cancelar </button>';
							echo '    <button type="button" name="sair" id="sair" onclick="loadPage(1);" class="btn btn-primary" > <i class="icon-home"></i> Sair </button> <input type="hidden" id="controle" value="gravar"> </td>';
						}
					echo '</tr>';
				echo '</table>';
			?>
		</form>
		<div id="retornoMunicipio"> </div>
		<div class="container">
				<h4>Favor preencha os campos abaixo citados:</h4>
				<ol>					
				</ol>
		</div>
	</body>
</html>